﻿using UnityEngine;
using System.Collections;
using System;

public class AssetsLoader : SingletonMono<AssetsLoader>
{
    private Action<byte[]> m_callback;

    public string GetPath(string path)
    {
        string p = "file://" + Application.streamingAssetsPath + path;
#if UNITY_EDITOR
#elif UNITY_ANDROID
         p = Application.streamingAssetsPath + path;
#endif
        return p;
    }

    public string GetPathExternal(string path)
    {
        string p = Application.streamingAssetsPath + path;
#if UNITY_EDITOR
#elif UNITY_ANDROID
         p = Application.streamingAssetsPath + path;
#endif
        return p;
    }

    public void LoadBytes(string path,Action<byte[]> onComplate)
    {
        m_callback = onComplate;
        StartCoroutine(Load(path));
    }

    IEnumerator Load(string path)
    {
        var p = GetPath(path);
        WWW www = new WWW(p);
        yield return www;
        if (string.IsNullOrEmpty(www.error))
        {
            while (www.isDone == false) yield return null;
            if (m_callback != null)
            {
                m_callback(www.bytes);
            }
        }
        else
        {
            Debug.LogError(www.error);
        }

    }
}
